package com.edu.base.rest;

import com.edu.base.common.utils.IdWorker;
import com.edu.base.ums.member.domain.ClsMember;
import com.edu.base.ums.member.service.IClsMemberService;
import com.edu.base.ums.memberAccountRecord.domain.ClsMemberAccountRecord;
import com.edu.base.ums.memberAccountRecord.mapper.ClsMemberAccountRecordMapper;
import com.edu.base.ums.memberAccountRecord.service.IClsMemberAccountRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 积分 
 * @author admin
 */
@Api(tags = "8、我的积分相关接口")
@RestController
@RequestMapping("/api/mine")
public class AppMemberAccountController extends BaseController
{
	@Autowired
	private IClsMemberAccountRecordService clsMemberAccountRecordService;
	@Autowired
	private ClsMemberAccountRecordMapper clsMemberAccountRecordMapper;
	 @Autowired
	 private IClsMemberService  clsMemberService;

 
    @GetMapping(value ="/getPoints")
   	@ApiOperation("获取会员积分信息")  
    @ApiImplicitParams({
 	    @ApiImplicitParam(name = "memberId", value = "会员ID"),
    })
    public AjaxResult list(String memberId)
    {
    	ClsMemberAccountRecord vo = new ClsMemberAccountRecord();
    	vo.setMemberId(memberId);
    	// 查询
        List<ClsMemberAccountRecord> list = clsMemberAccountRecordService.selectClsMemberAccountRecordList(vo);
        // TODO 获取会员总积分和积分消费明细 
        Long sumPoint = list.stream().mapToLong(ClsMemberAccountRecord::getChangePoint).sum(); 
        AjaxResult ajaxResult =  AjaxResult.success();
        ajaxResult.put("sumPoint", sumPoint);
        ajaxResult.put("list", list);
        return ajaxResult;
    }

    @PostMapping("/addPoint")
    @ApiOperation(value = "新增会员积分信息",hidden = true)
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "memberId", value = "当前用户ID",required = true),  
    	@ApiImplicitParam(name = "operateNote", value = "交易内容",required = true), 
    	@ApiImplicitParam(name = "changePoint", value = "交易积分",required = true), 
    	@ApiImplicitParam(name = "changeType", value = "交易类型 (金额：30001 充值/ 30002 充会员/ 30003消费/30004 退款/ 30004 提现; \"\r\n" + 
    			"    		+ \"							积分：40001:推荐/ 40002推荐人消费/ 40003消费/ 40004消费返积分)",required = true), 
    	
	})
    public AjaxResult addPoint(String memberId,String operateNote,Long changePoint,String changeType)
    {
    	ClsMember cls =  clsMemberService.selectClsMemberById(memberId);
    	if(cls == null) {
    		return AjaxResult.error("会员信息不存在");
    	}
    	ClsMemberAccountRecord vo = new ClsMemberAccountRecord();
    	vo.setId(IdWorker.idAutoIncrement());
    	vo.setCreateDate(DateUtils.getNowDate());
    	vo.setTransactionTime(new Date());
    	vo.setMemberId(memberId);
    	vo.setOperateNote(operateNote);
    	vo.setOperateMan(cls.getUserName());
    	vo.setChangePoint(changePoint); 
    	vo.setChangeType(Integer.valueOf(changeType));
    	vo.setTransactionTime(new Date());
     	clsMemberAccountRecordService.insertClsMemberAccountRecord(vo);
    	 
    	// TDOD 更新当前会员积分
     	if(cls.getIntegration() == null) {
        	cls.setIntegration(changePoint);
     	}else {
        	cls.setIntegration(cls.getIntegration() +changePoint);
     	}
        return AjaxResult.success();
    }
 
 
}
